﻿// -----------------------------------------------------------------------
// <copyright file="CancelResult.cs" company="Zen Design Corp">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Zen.BlackBerry.Push
{
	using System.Collections.Generic;
	using Zen.BlackBerry.Common;

	/// <summary>
	/// Represents the result of a cancel including the response from the PPG
	/// and sets of subscriber ids that were not sent to the PPG because they
	/// were invalid in some way (i.e. they do not exist, are inactive, or are
	/// suspended at the time of the cancel request).
	/// </summary>
	/// <remarks>
	/// Note: The three invalid subscriber sets are populated only if addresses
	/// were provided in the request and the push service is not storing push
	/// requests.
	/// </remarks>
	public class CancelResult
	{
		private ISet<string> _doNotExistSubscriberIds = new HashSet<string>();
		private ISet<string> _inactiveSubscriberIds = new HashSet<string>();
		private ISet<string> _suspendedSubscriberIds = new HashSet<string>();
		private List<CancelOutcome> _outcomes = new List<CancelOutcome>();

		/// <summary>
		/// Initializes a new instance of the <see cref="CancelResult"/> class.
		/// </summary>
		/// <param name="pushId">The push id.</param>
		public CancelResult(string pushId)
		{
			PushId = pushId;
		}

		/// <summary>
		/// Gets the push id.
		/// </summary>
		/// <value>The push id.</value>
		public string PushId
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets the collection of subscribers that could not be found
		/// in storage.
		/// </summary>
		/// <value>The do not exist subscriber ids.</value>
		public ISet<string> DoNotExistSubscriberIds
		{
			get
			{
				if (_doNotExistSubscriberIds == null)
				{
					_doNotExistSubscriberIds = new HashSet<string>();
				}
				return _doNotExistSubscriberIds;
			}
			set
			{
				_doNotExistSubscriberIds = value;
			}
		}

		/// <summary>
		/// Gets or sets the subscriber ids that match inactive subscribers.
		/// </summary>
		/// <value>The inactive subscriber ids.</value>
		public ISet<string> InactiveSubscriberIds
		{
			get
			{
				if (_inactiveSubscriberIds == null)
				{
					_inactiveSubscriberIds = new HashSet<string>();
				}
				return _inactiveSubscriberIds;
			}
			set
			{
				_inactiveSubscriberIds = value;
			}
		}

		/// <summary>
		/// Gets or sets the subscriber ids that match suspended subscribers.
		/// </summary>
		/// <value>The suspended subscriber ids.</value>
		public ISet<string> SuspendedSubscriberIds
		{
			get
			{
				if (_suspendedSubscriberIds == null)
				{
					_suspendedSubscriberIds = new HashSet<string>();
				}
				return _suspendedSubscriberIds;
			}
			set
			{
				_suspendedSubscriberIds = value;
			}
		}

		/// <summary>
		/// Gets the address-specific push outcomes.
		/// </summary>
		/// <value>The outcomes.</value>
		public IList<CancelOutcome> Outcomes
		{
			get
			{
				return _outcomes.AsReadOnly();
			}
		}

		/// <summary>
		/// Adds an outcome to the cancel result.
		/// </summary>
		/// <param name="address">The address.</param>
		/// <param name="code">The code.</param>
		public void AddOutcome(string address, StatusCode code)
		{
			_outcomes.Add(new CancelOutcome(address, code, code.Description));
		}

		/// <summary>
		/// Adds an outcome to the cancel result.
		/// </summary>
		/// <param name="address">The address.</param>
		/// <param name="code">The code.</param>
		/// <param name="description">The description.</param>
		public void AddOutcome(string address, StatusCode code, string description)
		{
			_outcomes.Add(new CancelOutcome(address, code, description));
		}
	}
}
